#coding=utf-8
import sys

def main():
    from uliweb import function
    from uliweb.manage import make_application
    import logging

    app = make_application(project_dir='..', start=False, debug_console=False, debug=False)
    redis = function('get_redis')()
    log = logging.getLogger('uliweb.app')
    while 1:
        send_mail(redis, log)

def send_mail(redis, log):
    from uliweb.mail import Mail
    from uliweb.utils.common import Serial
    from uliweb import settings

    record = redis.brpop('send_mails', 10)
    if record:
        mail = Serial.load(record[1])
        Mail().send_mail(**mail)
        log.info('Mail has been sent from %s to %r: %s' % (mail['from'], mail['to'], mail['title']))

if __name__ == '__main__':
    main()
